package indi.zhifa.engine.core.excel.entity;

import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hatak
 */
@Data
public class ExcelColumnTreeNode {
    protected Map<String,ExcelColumnTreeNode> children;
    protected HandlerInfo handlerInfo;
    protected boolean isLeaf;

    public ExcelColumnTreeNode(){
        children = new HashMap<>();
        handlerInfo = null;
        isLeaf = false;
    }

    public ExcelColumnTreeNode addKey(String pKey){
        ExcelColumnTreeNode node = children.get(pKey);
        if(null == node){
            node = new ExcelColumnTreeNode();
            children.put(pKey,node);
        }
        return node;
    }

    public ExcelColumnTreeNode addLeaf(String pKey,HandlerInfo handlerInfo){
        ExcelColumnTreeNode node = new ExcelColumnTreeNode();
        children.put(pKey,node);
        node.isLeaf = true;
        node.handlerInfo = handlerInfo;
        return node;
    }


    public HandlerInfo getExcelCellHandlerInfo(ExcelHeader pHeader){
        ExcelColumnTreeNode node = this;
        List<String> keys = pHeader.getKeys();
        for(int i=0;i<keys.size();i++){
            String key = keys.get(i);
            node = node.children.get(key);
            if(null == node){
                return null;
            }
            if(node.isLeaf){
                return node.handlerInfo;
            }
        }
        return null;
    }
}
